home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / mint / lib / mntlib44.zoo / mntlib / longlong.h < prev    next >
C/C++ Source or Header  |  1994-02-14  |  30KB  |  1,004 lines

  1. /* longlong.h -- definitions for mixed size 32/64 bit arithmetic.
  2.    Copyright (C) 1991, 1992 Free Software Foundation, Inc.
  3.  
  4.    This definition file is free software; you can redistribute it
  5.    and/or modify it under the terms of the GNU General Public
  6.    License as published by the Free Software Foundation; either
  7.    version 2, or (at your option) any later version.
  8.  
  9.    This definition file is distributed in the hope that it will be
  10.    useful, but WITHOUT ANY WARRANTY; without even the implied
  11.    warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12.    See the GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. #ifndef SI_TYPE_SIZE
  19. #define SI_TYPE_SIZE 32
  20. #endif
  21.  
  22. #define __BITS4 (SI_TYPE_SIZE / 4)
  23. #define __ll_B (1L << (SI_TYPE_SIZE / 2))
  24. #define __ll_lowpart(t) ((USItype) (t) % __ll_B)
  25. #define __ll_highpart(t) ((USItype) (t) / __ll_B)
  26.  
  27. /* Define auxiliary asm macros.
  28.  
  29.    1) umul_ppmm(high_prod, low_prod, multipler, multiplicand)
  30.    multiplies two USItype integers MULTIPLER and MULTIPLICAND,
  31.    and generates a two-part USItype product in HIGH_PROD and
  32.    LOW_PROD.
  33.  
  34.    2) __umulsidi3(a,b) multiplies two USItype integers A and B,
  35.    and returns a UDItype product.  This is just a variant of umul_ppmm.
  36.  
  37.    3) udiv_qrnnd(quotient, remainder, high_numerator, low_numerator,
  38.    denominator) divides a two-word unsigned integer, composed by the
  39.    integers HIGH_NUMERATOR and LOW_NUMERATOR, by DENOMINATOR and
  40.    places the quotient in QUOTIENT and the remainder in REMAINDER.
  41.    HIGH_NUMERATOR must be less than DENOMINATOR for correct operation.
  42.    If, in addition, the most significant bit of DENOMINATOR must be 1,
  43.    then the pre-processor symbol UDIV_NEEDS_NORMALIZATION is defined to 1.
  44.  
  45.    4) sdiv_qrnnd(quotient, remainder, high_numerator, low_numerator,
  46.    denominator).  Like udiv_qrnnd but the numbers are signed.  The
  47.    quotient is rounded towards 0.
  48.  
  49.    5) count_leading_zeros(count, x) counts the number of zero-bits from
  50.    the msb to the first non-zero bit.  This is the number of steps X
  51.    needs to be shifted left to set the msb.  Undefined for X == 0.
  52.  
  53.    6) add_ssaaaa(high_sum, low_sum, high_addend_1, low_addend_1,
  54.    high_addend_2, low_addend_2) adds two two-word unsigned integers,
  55.    composed by HIGH_ADDEND_1 and LOW_ADDEND_1, and HIGH_ADDEND_2 and
  56.    LOW_ADDEND_2 respectively.  The result is placed in HIGH_SUM and
  57.    LOW_SUM.  Overflow (i.e. carry out) is not stored anywhere, and is
  58.    lost.
  59.  
  60.    7) sub_ddmmss(high_difference, low_difference, high_minuend,
  61.    low_minuend, high_subtrahend, low_subtrahend) subtracts two
  62.    two-word unsigned integers, composed by HIGH_MINUEND_1 and
  63.    LOW_MINUEND_1, and HIGH_SUBTRAHEND_2 and LOW_SUBTRAHEND_2
  64.    respectively.  The result is placed in HIGH_DIFFERENCE and
  65.    LOW_DIFFERENCE.  Overflow (i.e. carry out) is not stored anywhere,
  66.    and is lost.
  67.  
  68.    If any of these macros are left undefined for a particular CPU,
  69.    C macros are used.  */
  70.  
  71. /* The CPUs come in alphabetical order below.
  72.  
  73.    Please add support for more CPUs here, or improve the current support
  74.    for the CPUs below!
  75.    (E.g. WE32100, i960, IBM360.)  */
  76.  
  77. #if defined (__GNUC__) && !defined (NO_ASM)
  78.  
  79. /* We sometimes need to clobber "cc" with gcc2, but that would not be
  80.    understood by gcc1.  Use cpp to avoid major code duplication.  */
  81. #if __GNUC__ < 2
  82. #define __CLOBBER_CC
  83. #define __AND_CLOBBER_CC
  84. #else /* __GNUC__ >= 2 */
  85. #define __CLOBBER_CC : "cc"
  86. #define __AND_CLOBBER_CC , "cc"
  87. #endif /* __GNUC__ < 2 */
  88.  
  89. #if defined (__a29k__) || defined (___AM29K__)
  90. #define add_ssaaaa(sh, sl, ah, al, bh, bl) \
  91.   __asm__ ("add %1,%4,%5
  92.     addc %0,%2,%3"                            \
  93.        : "=r" ((USItype)(sh)),                    \
  94.         "=&r" ((USItype)(sl))                    \
  95.        : "%r" ((USItype)(ah)),                    \
  96.          "rI" ((USItype)(bh)),                    \
  97.          "%r" ((USItype)(al)),                    \
  98.          "rI" ((USItype)(bl)))
  99. #define sub_ddmmss(sh, sl, ah, al, bh, bl) \
  100.   __asm__ ("sub %1,%4,%5
  101.     subc %0,%2,%3"                            \
  102.        : "=r" ((USItype)(sh)),                    \
  103.          "=&r" ((USItype)(sl))                    \
  104.        : "r" ((USItype)(ah)),                    \
  105.          "rI" ((USItype)(bh)),                    \
  106.          "r" ((USItype)(al)),                    \
  107.          "rI" ((USItype)(bl)))
  108. #define umul_ppmm(xh, xl, m0, m1) \
  109.   do {                                    \
  110.     USItype __m0 = (m0), __m1 = (m1);                    \
  111.     __asm__ ("multiplu %0,%1,%2"                    \
  112.          : "=r" ((USItype)(xl))                    \
  113.          : "r" (__m0),                        \
  114.            "r" (__m1));                        \
  115.     __asm__ ("multmu %0,%1,%2"                        \
  116.          : "=r" ((USItype)(xh))                    \
  117.          : "r" (__m0),                        \
  118.            "r" (__m1));                        \
  119.   } while (0)
  120. #define udiv_qrnnd(q, r, n1, n0, d) \
  121.   __asm__ ("dividu %0,%3,%4"                        \
  122.        : "=r" ((USItype)(q)),                    \
  123.          "=q" ((USItype)(r))                    \
  124.        : "1" ((USItype)(n1)),                    \
  125.          "r" ((USItype)(n0)),                    \
  126.          "r" ((USItype)(d)))
  127. #define count_leading_zeros(count, x) \
  128.     __asm__ ("clz %0,%1"                        \
  129.          : "=r" ((USItype)(count))                    \
  130.          : "r" ((USItype)(x)))
  131. #endif /* __a29k__ */
  132.  
  133. #if defined (__arm__)
  134. #define add_ssaaaa(sh, sl, ah, al, bh, bl) \
  135.   __asm__ ("adds %1,%4,%5
  136.     adc %0,%2,%3"                            \
  137.        : "=r" ((USItype)(sh)),                    \
  138.          "=&r" ((USItype)(sl))                    \
  139.        : "%r" ((USItype)(ah)),                    \
  140.          "rI" ((USItype)(bh)),                    \
  141.          "%r" ((USItype)(al)),                    \
  142.          "rI" ((USItype)(bl)))
  143. #define sub_ddmmss(sh, sl, ah, al, bh, bl) \
  144.   __asm__ ("subs %1,%4,%5
  145.     sbc %0,%2,%3"                            \
  146.        : "=r" ((USItype)(sh)),                    \
  147.          "=&r" ((USItype)(sl))                    \
  148.        : "r" ((USItype)(ah)),                    \
  149.          "rI" ((USItype)(bh)),                    \
  150.          "r" ((USItype)(al)),                    \
  151.          "rI" ((USItype)(bl)))
  152. #endif /* __arm__ */
  153.  
  154. #if defined (__gmicro__)
  155. #define add_ssaaaa(sh, sl, ah, al, bh, bl) \
  156.   __asm__ ("add.w %5,%1
  157.     addx %3,%0"                            \
  158.        : "=g" ((USItype)(sh)),                    \
  159.          "=&g" ((USItype)(sl))                    \
  160.        : "%0" ((USItype)(ah)),                    \
  161.          "g" ((USItype)(bh)),                    \
  162.          "%1" ((USItype)(al)),                    \
  163.          "g" ((USItype)(bl)))
  164. #define sub_ddmmss(sh, sl, ah, al, bh, bl) \
  165.   __asm__ ("sub.w %5,%1
  166.     subx %3,%0"                            \
  167.        : "=g" ((USItype)(sh)),                    \
  168.          "=&g" ((USItype)(sl))                    \
  169.        : "0" ((USItype)(ah)),                    \
  170.          "g" ((USItype)(bh)),                    \
  171.          "1" ((USItype)(al)),                    \
  172.          "g" ((USItype)(bl)))
  173. #define umul_ppmm(ph, pl, m0, m1) \
  174.   __asm__ ("mulx %3,%0,%1"                        \
  175.        : "=g" ((USItype)(ph)),                    \
  176.          "=r" ((USItype)(pl))                    \
  177.        : "%0" ((USItype)(m0)),                    \
  178.          "g" ((USItype)(m1)))
  179. #define udiv_qrnnd(q, r, nh, nl, d) \
  180.   __asm__ ("divx %4,%0,%1"                        \
  181.        : "=g" ((USItype)(q)),                    \
  182.          "=r" ((USItype)(r))                    \
  183.        : "1" ((USItype)(nh)),                    \
  184.          "0" ((USItype)(nl)),                    \
  185.          "g" ((USItype)(d)))
  186. #define count_leading_zeros(count, x) \
  187.   __asm__ ("bsch/1 %1,%0"                        \
  188.        : "=g" (count)                        \
  189.        : "g" ((USItype)(x)),                    \
  190.          "0" ((USItype)0))
  191. #endif
  192.  
  193. #if defined (__hppa)
  194. #define add_ssaaaa(sh, sl, ah, al, bh, bl) \
  195.   __asm__ ("add %4,%5,%1
  196.     addc %2,%3,%0"                            \
  197.        : "=r" ((USItype)(sh)),                    \
  198.          "=&r" ((USItype)(sl))                    \
  199.        : "%rM" ((USItype)(ah)),                    \
  200.          "rM" ((USItype)(bh)),                    \
  201.          "%rM" ((USItype)(al)),                    \
  202.          "rM" ((USItype)(bl)))
  203. #define sub_ddmmss(sh, sl, ah, al, bh, bl) \
  204.   __asm__ ("sub %4,%5,%1
  205.     subb %2,%3,%0"                            \
  206.        : "=r" ((USItype)(sh)),                    \
  207.          "=&r" ((USItype)(sl))                    \
  208.        : "rM" ((USItype)(ah)),                    \
  209.          "rM" ((USItype)(bh)),                    \
  210.          "rM" ((USItype)(al)),                    \
  211.          "rM" ((USItype)(bl)))
  212. #if defined (_PA_RISC1_1)
  213. #define umul_ppmm(w1, w0, u, v) \
  214.   do {                                    \
  215.     union                                \
  216.       {                                    \
  217.     UDItype __f;                            \
  218.     struct {USItype __w1, __w0;} __w1w0;                \
  219.       } __t;                                \
  220.     __asm__ ("xmpyu %1,%2,%0"                        \
  221.          : "=x" (__t.__f)                        \
  222.          : "x" ((USItype)(u)),                    \
  223.            "x" ((USItype)(v)));                    \
  224.     (w1) = __t.__w1w0.__w1;                        \
  225.     (w0) = __t.__w1w0.__w0;                        \
  226.      } while (0)
  227. #define UMUL_TIME 8
  228. #else
  229. #define UMUL_TIME 30
  230. #endif
  231. #define UDIV_TIME 40
  232. #endif
  233.  
  234. #if defined (__i386__) || defined (__i486__)
  235. #define add_ssaaaa(sh, sl, ah, al, bh, bl) \
  236.   __asm__ ("addl %5,%1
  237.     adcl %3,%0"                            \
  238.        : "=r" ((USItype)(sh)),                    \
  239.          "=&r" ((USItype)(sl))                    \
  240.